package enstb.web.client;

import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Chronometer;

public class ModePresentation extends Activity {
	/** Called when the activity is first created. */
	private static final int REQUEST_ENABLE_BT = 2;
	private Chronometer chronometer;
	private String user = "ychen02";
	private AlertDialog dlg;

	// Member fields
	private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
			.getDefaultAdapter();
	private BluetoothDevice yBluetoothDevice = null;
	private PresentationService pService = new PresentationService();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button buttonLeft = (Button) findViewById(R.id.buttonleft);
		Button buttonRight = (Button) findViewById(R.id.buttonright);
		Button buttonStart = (Button) findViewById(R.id.buttonstart);
		Button buttonExit = (Button) findViewById(R.id.buttonexit);
		chronometer = (Chronometer) findViewById(R.id.chronometer);
		LayoutInflater factory = LayoutInflater.from(ModePresentation.this);
		View textEntryView = factory.inflate(R.layout.dialog, null);
		dlg = new AlertDialog.Builder(ModePresentation.this)
				.setTitle("user to connect").setView(textEntryView).setPositiveButton(
						"OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								EditText edit_text = (EditText)dlg.findViewById(R.id.password_edit);
								Log.d("INFO","le edit text  est "+edit_text.toString()); 
								user =edit_text.getText().toString();
								connect();	
							}
						}).setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								connect();
							}
						}).create();
		dlg.show();


		buttonLeft.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				Toast.makeText(ModePresentation.this, "Left",
						Toast.LENGTH_SHORT).show();
				sendMessage(1);
			}
		});
		buttonRight.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				Toast.makeText(ModePresentation.this, "Right",
						Toast.LENGTH_SHORT).show();
				sendMessage(2);

			}
		});
		buttonStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				Toast.makeText(ModePresentation.this, "Start",
						Toast.LENGTH_SHORT).show();
				sendMessage(3);
				chronometer.setBase(SystemClock.elapsedRealtime());
				chronometer.start();
			}
		});
		buttonExit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				Toast
						.makeText(ModePresentation.this, "esc",
								Toast.LENGTH_SHORT).show();
				sendMessage(4);
				chronometer.setBase(SystemClock.elapsedRealtime());
				chronometer.stop();
			}
		});

	}

	public void connect() {
		if (mBluetoothAdapter != null) {
			Toast.makeText(this, "Bluetooth is  available", Toast.LENGTH_LONG)
					.show();

			// Device does not support Bluetooth
		}
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
				.getBondedDevices();
		// If there are paired devices
		if (pairedDevices.size() > 0) {
			// Loop through paired devices
			for (BluetoothDevice device : pairedDevices) {
				// Add the name and address to an array adapter to show in a
				// ListView
				if (device.getName().equals(user)) {
					this.yBluetoothDevice = device;
					Toast
							.makeText(ModePresentation.this,
									device.getName() + " is finded",
									Toast.LENGTH_SHORT).show();
					Log.d("DEBUG", "les infos de device à conneter: "
							+ device.getName() + "   " + device.getAddress()
							+ "class" + device.getBluetoothClass());
					pService.connect(device);
				}

			}
		}

	}

	public void sendMessage(int message) {

		// Check that there's actually something to send
		pService.write(message);

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_ENABLE_BT) {

			if (resultCode == RESULT_OK) {

				Toast
						.makeText(this, "Bluetooth is  enabled",
								Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "Bluetooth is  not enabled",
						Toast.LENGTH_LONG).show();
			}

		}

	}

}